package com.sq.universe.base.utils;

import cn.hutool.core.net.NetUtil;
import cn.hutool.setting.Setting;
import com.alibaba.fastjson.JSON;
import com.sq.universe.log.entity.LogCenterEntity;
import com.sq.universe.log.feign.LogFeign;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveTask;

public class LogTask extends RecursiveTask<Integer> {

    private static final int THRESHOLD = 20;
    private List<String> msgArr;
    private LogFeign logFeign;
    private int end;

    public LogTask( List<String> msgArr,LogFeign logFeign ) {
        this.msgArr = msgArr;
        this.logFeign = logFeign;
    }

    @Override
    protected Integer compute() {
        if(msgArr.size() <= 20){
            Setting setting = new Setting("tlog.setting");
            LogCenterEntity logCenterEntity = new LogCenterEntity();
            logCenterEntity.setIp(JSON.toJSONString(NetUtil.localIpv4s()));
            logCenterEntity.setSystem(setting.getStr("applicationName"));
            if(msgArr.size() > 0){
                for(String _msg : msgArr){
                    logCenterEntity.setMsg(_msg);
                    logFeign.receiveLog(logCenterEntity);
                }
            }
        }else{
            LogTask leftTask = new LogTask(new ArrayList<>(msgArr.subList(0,msgArr.size() / 2)), logFeign);
            LogTask rightTask = new LogTask(new ArrayList<>(msgArr.subList(msgArr.size() / 2,msgArr.size() - 1)), logFeign);
            //执行子任务
            leftTask.fork();
            rightTask.fork();
        }
        return null;
    }
}
